/**
 * Created by CYT on 2017/10/10.
 */


$(document).ready(function () {
    var userRealName;
    var userName;
    var userPhone;
    var userEmail;
    var password;
    var re_password;
    var userSchool;
    var userNickname;
    var mark=false;

    $("#realName").blur(function () {
        mark = false;
        userRealName = $("#realName").val();
        if (!/^[\u4e00-\u9fa5]{0,}$/.test(userRealName)) {/*判断真实姓名：只能中文*/
            $("#p_realName").text("只能输入中文，请重新输入");
            $("#realName").val();
            mark = false;
        }
        else {
            $("#p_realName").text("");
            mark = true;
        }
    })

    $("#name").blur(function () {
        mark = false;
        userName = $("#name").val();
        if (!/^[A-Za-z0-9]+$/.test(userName)) {/*判断用户名：只支持英文和数字*/
            $("#p_name").text("只能输入英文和数字，请重新输入");
            $("#name").val();
            mark = false;
        }
        else {
            $.ajax({
                url: "/coolprint/print_cyt/findUserName",
                type: "post",
                data: {
                    userName: userName
                },
                success: function (data) {
                    if (data === true) {/*用户名已存在*/
                        $("#p_name").text("用户名已存在，请更换用户名");
                        mark = false;
                    }
                    else {
                        mark = true;
                        $("#p_name").text("");
                    }
                }
            })
        }
    })

    $("#phone").blur(function () {
        mark = false;
        userPhone = $("#phone").val();
        if (!/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(userPhone)) {/*判断手机号*/
            $("#p_phone").text("请输入的正确的手机号");
            $("#phone").val();
            mark = false;
        }
        else {
            $.ajax({
                url: "/coolprint/print_cyt/findUserPhone",
                type: "post",
                data: {
                    userPhone: userPhone
                },
                success: function (data) {
                    if (data === true) {/*手机号已存在*/
                        $("#p_phone").text("手机号已存在，请更换手机号");
                        mark = false;
                    }
                    else {
                        $("#p_phone").text("");
                        mark = true;
                    }
                }
            })
        }
    })
    $("#email").blur(function () {
        mark = false;
        userEmail = $("#email").val();
        if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(userEmail)) {/*判断邮箱*/
            $("#p_email").text("请输入正确的邮箱格式");
            $("#email").val();
            mark = false;
        }
        else {
            $.ajax({
                url: "/coolprint/print_cyt/findUserEmail",
                type: "post",
                data: {
                    userEmail: userEmail
                },
                success: function (data) {
                    if (data === true) {/*Email已存在*/
                        $("#p_email").text("邮箱已存在，请更换邮箱");
                        mark = false;
                    }
                    else {
                        $("#p_email").text("");
                        mark = true;
                    }
                }
            })
        }
    })
    $("#password").blur(function () {
        mark = false;
        password = $("#password").val();
        if (!/^.{6,}$/.test(password)) {/*判断密码：至少6个字符*/
            $("#p_password").text("至少输入6个字符");
            $("#password").val();
            mark = false;
        }
        else {
            $("#p_password").text("");
            mark = true;
        }
    })
    $("#re_password").blur(function () {
        mark = false;
        re_password = $("#re_password").val();
        if (password !== re_password) {
            $("#p_re_password").text("密码不一致，请重新输入");
            $("#re_password").val();
            mark = false;
        }
        else {
            $("#p_re_password").text("");
            mark = true;
        }
    })

    $("#zhuce").click(function () {
        if (mark === true) {
            //所有信息正确，开始插入数据。
            userRealName = $("#realName").val();
            userName = $("#name").val();
            userPhone = $("#phone").val();
            userEmail = $("#email").val();
            password = $("#password").val();
            userSchool = $("#school").val();
            userNickname = userName;
            //默认用户名是昵称
            $.ajax({
                url: "/coolprint/print_cyt/insertUserInfo",
                type: "post",
                dataType: "json",
                data: {
                    userNickname: userNickname,
                    userName: userName,
                    userRealName: userRealName,
                    userPhone: userPhone,
                    userEmail: userEmail,
                    userSchool: userSchool,
                    userCode: password
                },
                success: function (data) {
                    if (data.ajaxInfo === "Success") {
                        alert("注册成功，请重新登陆");
                        window.location.href = "../logIns/logIn.html"
                    }
                }
            })
        }
    })
})
